
// Write your code in the same way as for native WebSocket:
// var ws = new io.connect("wss://116.62.20.199:8088/webrtc");
var ws = new io.connect("wss://172.16.3.232:8088/webrtc");
// var ws = new io.connect("wss://192.168.0.40:3002/webrtc");
// 测试连接
ws.on('sendToClient', function (e) {
    document.getElementById('text1').value += e + '\n';
})
ws.emit('sentToServer');

ws.on('message', function (e) {
    console.log(e)
    document.getElementById('text1').value += e + '\n';
})

getAllSocket();
getAllRoom();
// setInterval(() => {
//     getAllSocket();
//     getAllRoom();
// }, 3000);

function but1(e) {
    ws.emit('send', document.getElementById('input1').value)
}
function but2(e) {
    ws.emit('broadcastRoom', document.getElementById('input1').value)
}
function but6(e) {
    myWebrtc.dataChannal.send(document.getElementById('input1').value);
}
// 所有的socket
var allS = document.getElementById('allS');
ws.on('resSocket', function (e) {
    allS.innerText = '';
    e.forEach(i => {
        var li = document.createElement("li");
        li.innerHTML = i;
        if (ws.id != i) {
            var a = document.createElement("button");
            a.innerText = '开始建立连接';
            a.onclick = () => {
                myWebrtc.establish(i);
            }
            li.appendChild(a);
        }
        allS.appendChild(li);
    });
    console.log(e)
})
// 所有的room
var allR = document.getElementById('allR');
ws.on('resRoom', function (e) {
    allR.innerText = '';
    e.forEach(i => {
        var li = document.createElement("li");
        li.innerHTML = i;
        if (ws.id != i) {
            var b = document.createElement("button");
            b.innerText = '加入此房间';
            b.onclick = () => {
                ws.emit('joinRoom', i)
            }
            li.appendChild(b);

            var c = document.createElement("button");
            c.innerText = '删除此房间';
            c.onclick = () => {
                ws.emit('removeRoom', i)
            }
            li.appendChild(c);
        }
        allR.appendChild(li);
    });
    console.log(e)
})

ws.on('setSDP', function (e) {
    if (e.type == 'video-offer') {
        myWebrtc.createAnswer(e);
    } else if (e.type == 'video-answer') {
        myWebrtc.setRemoteSDP(e);
    }
})
ws.on('setCandidate', function (e) {
    myWebrtc.addCandidate(e);
})


function getAllSocket() {
    ws.emit('allSocket');
}
function getAllRoom() {
    ws.emit('allRoom');
}
function createRoom() {
    ws.emit('createRoom');
}


